from pyb import Pin, ExtInt
import pwsutil

class RAIN:
	def __init__(self):
		self._lasttime = pwsutil.time_count
		_rain1 = pwsutil.readBKSRAM(2)
		_rain2 = pwsutil.readBKSRAM(3)
		if (_rain1 != (~_rain2)):
			pwsutil.writeBKSRAM(2, 0)
			pwsutil.writeBKSRAM(3, ~0)
			self._rain = 0
		else:
			self._rain = _rain1

		ExtInt(Pin('B5'), ExtInt.IRQ_FALLING, Pin.PULL_UP, self._extint_cb)

	def getRain(self):
		pwsutil.writeBKSRAM(2, self._rain)
		pwsutil.writeBKSRAM(3, ~self._rain)
		return self._rain

	def _extint_cb(self, l):
		if (pwsutil.time_count >= (self._lasttime + 2)):
			self._lasttime = pwsutil.time_count
			self._rain += 5		# 0.5mm


	